﻿using System;
using System.Reflection.Emit;

namespace RuntimeTypeGenerator
{
    /// <summary>
    /// Defines an interface for building types dynamically using the <see cref="ModuleBuilder"/>.
    /// </summary>
    public interface ITypeBuilder
    {
        /// <summary>
        /// Gets the name used to identify the type produced by this builder.
        /// </summary>
        string TypeName { get; }
        /// <summary>
        /// Builds the type into the provided <paramref name="moduleBuilder"/>.
        /// </summary>
        /// <param name="moduleBuilder">The target of the type build.</param>
        /// <returns>The built type.</returns>
        Type BuildType(ModuleBuilder moduleBuilder);
    }

    /// <summary>
    /// Extension methods for <see cref="ITypeBuilder"/>
    /// </summary>
    public static class TypeBuilderExtensions
    {
        /// <summary>
        /// Build a type into the <paramref name="buildTarget"/>.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="buildTarget">The build target.</param>
        /// <returns>The built type.</returns>
        public static Type BuildTo(this ITypeBuilder builder, ITypeBuildTarget buildTarget)
        {
            return buildTarget.BuildWith(builder);
        }
    }
}
